package com.sugon.controller.device;

import com.sugon.annotation.AutoLog;
import com.sugon.entity.DevFileEntity;
import com.sugon.service.DevFileService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备图片信息Controller
 *
 * @author sugon
 * @date 2019-11-28 17:06:44
 */
@Controller
@RequestMapping("devfile")
public class DevFileController {
    @Autowired
    private DevFileService devFileService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
/*    @RequiresPermissions("devfile:list")*/
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<DevFileEntity> devFileList = devFileService.queryList(query);
        int total = devFileService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(devFileList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("devfile:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        DevFileEntity devFile = devFileService.queryObject(id);

        return R.ok().put("devFile", devFile);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("devfile:save")
    @ResponseBody
    public R save(@RequestBody DevFileEntity devFile) {
        //如果已经存在主位图片，则添加失败。
        Map<String, Object> map =new HashMap<String, Object>();
        map.put("fileType","1");
        map.put("devId",devFile.getDevId());
        List<DevFileEntity> list=devFileService.queryList(map);
        if(list.size()>0&&devFile.getFileType()==1){
            return R.error(42,"主位图片只能有一个");

        }else {
            devFileService.save(devFile);

            return R.ok();
        }


    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("devfile:update")
    @ResponseBody
    public R update(@RequestBody DevFileEntity devFile) {
        devFileService.update(devFile);

        return R.ok();
    }

    /**
     * 删除
     */
    @AutoLog("设备图片信息--删除")
    @RequestMapping("/delete")
    @RequiresPermissions("devfile:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        devFileService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<DevFileEntity> list = devFileService.queryList(params);

        return R.ok().put("list", list);
    }
}
